﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BOMS.DAL;
using System.Data;

namespace BOMS.WebUI
{
    public partial class Menu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string userId = HttpContext.Current.User.Identity.Name;
                showMenu(userId);
            }
        }
        protected void BtnRefresh_RefreshWinceTree(object sender, EventArgs e)
        {
            bindWince();
        }

        private void bindWince(){
            this.tvWince.Nodes.Clear();//先清空根下面的wince子节点

            TreeNode root = new TreeNode("现场主机");
          
            DataTable dtLan = SysAdmin.getParameter();
            string LanName = dtLan.Rows[0]["LanName"].ToString();
            root.Text = LanName;
            root.Value = LanName;
            root.NavigateUrl = "javascript:top.myTab.Cts('" + LanName + "','Web/RealStatus/groupPage.aspx?lanname=" + HttpUtility.UrlEncodeUnicode(LanName) + "',true)";
            this.tvWince.Nodes.Add(root);


            DataTable dt = SysAdmin.getWinceOfLan();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
TreeNode node=new TreeNode();
                string winceName=dt.Rows[i]["Hostname"].ToString();
                string winceIp=dt.Rows[i]["ip"].ToString();
                string winceMac = dt.Rows[i]["mac"].ToString();
                node.Text=winceName;
                node.Value = winceMac;
                node.NavigateUrl = "javascript:top.myTab.Cts('" + winceName + "','Web/RealStatus/groupDetail.aspx?winceIp=" + winceIp + "&winceMac=" + winceMac + "&lanName=" + HttpUtility.UrlEncodeUnicode(LanName) + "&winceName=" + HttpUtility.UrlEncodeUnicode(winceName) + "',true)";
root.ChildNodes.Add(node);     
            }
        }
        private void showMenu(string userId)
        {
            string moduleList = SysAdmin.getModuleListByUserId(userId);
            DataTable dt = SysAdmin.getAllModules();
            string[] moduleArr = moduleList.Split(',');

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string id = dt.Rows[i]["id"].ToString();
                string name = dt.Rows[i]["name"].ToString();
                if (Array.Exists(moduleArr, element => element.Equals(id)))
                {
                    switch (name)
                    {
                        case "现场主机":
                            bindWince();
                            this.tvWince.Visible = true;
                            break;
                        //case "停发电调度":
                        //    this.tvPowerControl.Visible = true;
                        //    break;
                        //case "电池组维护":
                        //    this.tvBatgrpPM.Visible = true;
                        //    break;
                        case "报警中心":
                            this.tvWarning.Visible = true;
                            break;
                        case "数据分析":
                            this.tvData.Visible = true;
                            break;
                        case "系统设置":
                            this.tvSystem.Visible = true;
                            break;
                        case "个人中心":
                            this.tvUser.Visible = true;
                            break;
                    }
                }
            }
        }
    }
}